<?php
//Knowledge Management Controller By Teerapong Singthong.
class Cops extends Controller{
	public $data 	= 	array();
	public function __construct()
	{
		parent::Controller();
		
		$this->load->helper('ckeditor');
		//Ckeditor's configuration
		$this->data['ckeditor'] = array(
		'id' 	=> 	'content',
		'path'	=>	'js/ckeditor',
		//Optionnal values
		'config' => array(
		'toolbar' 	=> 	array(	//Setting a custom toolbar
			array('Save','NewPage','Preview'),
			array('-','Maximize'),
			array('Link','Unlink'),
			array('Smiley')
			),
		'width' 	=> 	"560px",	//Setting a custom width
		'height' 	=> 	'80px',	//Setting a custom height
		),
		//Replacing styles from the "Styles tool"
		'styles' => array(
		//Creating a new style named "style 1"
		'style 1' => array (
		'name' 		=> 	'Blue Title',
		'element' 	=> 	'h2',
		'styles' => array(
		'color' 	=> 	'Blue',
		'font-weight' 	=> 	'bold'
		)
			),
		//Creating a new style named "style 2"
		'style 2' => array (
		'name' 	=> 	'Red Title',
		'element' 	=> 	'h2',
		'styles' => array(
		'color' 		=> 	'Red',
		'font-weight' 		=> 	'bold',
		'text-decoration'	=> 	'underline'
		)
			)
			)
			);
		$this->data['ckeditor_2'] = array(
		//ID of the textarea that will be replaced
		'id' 	=> 	'content_2',
		'path'	=>	'js/ckeditor',
		//Optionnal values
		'config' => array(
		'width' 	=> 	"865px",	//Setting a custom width
		'height' 	=> 	'150px',	//Setting a custom height
		'toolbar' 	=> 	array(	//Setting a custom toolbar
		array('Source','-','Save','NewPage','Preview','-','Templates'),
		array('Cut','Copy','Paste','PasteText','PasteFromWord','-','Print', 'SpellChecker', 'Scayt'),
		array('Undo','Redo','-','Bold', 'Italic'),
		array('Underline', 'Strike'),
		array('Find','Replace','-','SelectAll','RemoveFormat'),
		array('NumberedList','BulletedList','-','Outdent','Indent','Blockquote','CreateDiv'),
		array('JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'),
		array('Link','Unlink','Anchor'),
		array('Image','Table','HorizontalRule','Smiley','SpecialChar','PageBreak'),
		array('Styles','Format','Font','FontSize'),
		array('TextColor','BGColor'),
		array('-','Maximize'),
		'/'
		)
			),
		//Replacing styles from the "Styles tool"
		'styles' => array(
		//Creating a new style named "style 1"
		'style 3' => array (
		'name' 		=> 	'Green Title',
		'element' 	=> 	'h3',
		'styles' => array(
		'color' 	=> 	'Green',
		'font-weight' 	=> 	'bold'
		)
			)
			)
			);
	}
	
	function index(){
		$data["title"] = "CopS (Community of Pratice).";	//set title.
		
		//load pagination class
		$this->load->library('pagination');

		$offset=$this->uri->segment(3);

        $limit=2;

        $this->db->limit($limit, $offset);
		
        $this->db->join('membership','membership.member_id=cops_group.owner');
		$this->db->order_by('create_date','desc');
		$q_list_cops = $this->db->get('cops_group');

        $config['base_url'] = base_url().'/cops/index/';
        $config['total_rows'] = $this->db->count_all('cops_group');
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);
        $paginator=$this->pagination->create_links();

        $data = array(
            'query' => $q_list_cops,
            'paginator'=>$paginator,
			'title' => 'Organization - Main Page.',
			'navigator' => '&nbsp;&#8250;&nbsp;Community of Practice Node'
        );
		
		//view
		$this->load->view('inc/cops/main_cops',$data);	//load view one cols.
	}
	
	function create_cops(){
		$this->data['title'] = "Create New Cops Group";
		$this->data["navigator"] = "&nbsp;&#8250;&nbsp;Community of Practice Node&nbsp;&#8250;&nbsp;Create New group";
		$this->load->view('inc/cops/form_create',$this->data);
	}
	
	function add_cops(){
		$this->load->model('cops_model');
		$query = $this->cops_model->add_cops();
		
		if($query){
			echo "ok";
		}
		else
		{
			echo "false";
		}
	}
	
	function views_group_detail_by_id($id){
		$this->data['title'] = "Group Details";
		$this->data['id'] = $id;
		$this->data['navigator'] = "&nbsp;&#8250;&nbsp;Community of Practice Node&nbsp;&#8250;&nbsp;Group detail";
		$this->load->view('inc/cops/group_details',$this->data);
	}
	
	function upload_logo() {
			if(isset($_POST['upload'])) {
						  
			  //$path = base_url().'km_resource/member_display';
			  $config['upload_path'] = './km_resource/cops_group/logo/';
			  $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
			  $config['max_size']  = '0';
			  $config['max_width']  = '0';
			  $config['max_height']  = '0';
			  /* Load the upload library */
			  $this->load->library('upload', $config);
		
			  /* Create the config for image library */
			  /* (pretty self-explanatory) */
			  $configThumb = array();
			  $configThumb['image_library'] = 'gd2';
			  $configThumb['source_image'] = '';
			  $configThumb['create_thumb'] = TRUE;
			  $configThumb['maintain_ratio'] = TRUE;
			  
			  $configThumb['width'] = 135;
			  $configThumb['height'] = 145;
			  
			  /* Load the image library */
			  $this->load->library('image_lib');
		
			  for($i = 1; $i < 2; $i++) {
				$upload = $this->upload->do_upload('image'.$i);
				if($upload === FALSE) continue;
				$data = $this->upload->data();
				$uploadedFiles[$i] = $data;
				
				if($data['is_image'] == 1) {
				  $configThumb['source_image'] = $data['full_path'];
				  $this->image_lib->initialize($configThumb);
				  $this->image_lib->resize(); 
				}
			  }
			 	/*echo "<ul>";
					foreach($this->upload->data() as $item => $value){
						echo "<li>";
						echo $item;
						echo $value;
						echo "</li>";
					}
				echo "</ul>";*/
				
				//send to model.
				$data['file_name'];
				$data['file_ext'];
				$data['raw_name'];
				
				$data['group_id'] = $this->input->post('id');
				
				$this->load->model('cops_model');
				$cops_logo = $this->cops_model->upload_logo($data); 
				
				if($cops_logo){
					$path = base_url()."cops/groups/".$data['group_id'];
					redirect($path);
				}
			}					
		}
		
		function join_group_agreement($id){
			$data['title'] = "Join Group Agreement";
			$data['id'] = $id;
			$this->load->view('inc/cops/agreement',$data);
		}
		
		function into_group($id){
			$data['id'] = $id;
			$this->load->model('cops_model');
			$query = $this->cops_model->add_member_to_group($data);
			
			if($query){
				$path = base_url()."cops/groups/".$id;
				redirect($path);
			}
			else
			{
				echo "false";
			}
		}
		
		function leave_group_by_id($id){
			$data['member_id'] = $this->session->userdata('member_id');
			$data['group_id'] = $id;
			$this->load->model('cops_model');
			$query = $this->cops_model->leave_group_by_id($data);
			
			if($query){
				$path = base_url()."cops";
				redirect($path);
			}
			else
			{
				echo "false";
			}
		}
		
		function delete_group_by_id($id){
			$data['group_id'] = $id;
			
			$this->load->model('cops_model');
			$query = $this->cops_model->delete_group_by_id($data);
			
			if($query){
				$path = base_url()."cops";
				redirect($path);
			}
			else
			{
				echo "false";
			}
		}
		
		function discuss_post(){
			$this->load->model('cops_model');
			$query = $this->cops_model->discuss_post();
			
			if($query){
				$path = base_url()."cops/groups/".$this->input->post('group_id');
				redirect($path);
			}
			else
			{
				echo "flase;";
			}
		}
		
		function discuss_del($id,$id2){
			$data['discuss_id'] = $id2;
			$data['groups_id'] = $id;
			$this->load->model('cops_model');
			$query = $this->cops_model->discuss_del($data);
			
			if($query){
				$path = base_url()."cops/groups/".$data['groups_id']."/";
				redirect($path);
			}
			else
			{
				echo "false";
			}
		}
		
		function show_discuss_by_group_id($id){
			$data['id'] = $id;
			$data['title'] = "Views discuss.";
			$data['navigator'] = "&nbsp;&#8250;&nbsp;Community of Practice Node&nbsp;&#8250;&nbsp;Group detail&nbsp;&#8250;&nbsp;Show discuss";
			$this->load->view('inc/cops/view_discuss_by_group_id',$data);
		}
		
		function upload_file_to_group_id($id){
			$data['id'] = $id;
			$data['title'] = "Upload Files to Group.";
			$data['navigator'] = "&nbsp;&#8250;&nbsp;Community of Practice Node&nbsp;&#8250;&nbsp;Group detail&nbsp;&#8250;&nbsp;Upload files";
			$this->load->view('inc/cops/upload_file_to_group_id',$data);
		}
		
		function do_upload(){
			$config['upload_path'] = "./km_resource/cops_group/file_uploads";
			$config['allowed_types'] = 'doc|docx|docm|dotx|dotm|pptx|pptm|pdf|txt|htm|html|zip|gif|png|jpg';	//file allow
			$config['max_size']	= '1024';	//1mb
			$config['overwrite'] = TRUE;
			$config['remove_spaces'] = TRUE;	//replace with underscore
			$config['encrypt_name'] = TRUE;	//encrtyp file name :)
			
			$this->load->library('upload', $config);
		
			if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());
				//$this->load->view('upload_form', $error);
				echo "erro";
			}	
			else
			{
				//$data = array('upload_data' => $this->upload->data());
				$data = $this->upload->data();
				$this->load->model('cops_model');
				$query = $this->cops_model->insert_files($data);
				
				if($query){
					echo "ok";
				}
				else
				{
					echo "false";
				}
				//$this->load->view('upload_success', $data);
			}
		}
		
		function download_files($path){
			$d_file_name = base64_decode($path);	//file name
			$path = './km_resource/cops_group/file_uploads/'.$d_file_name;

			$this->zip->read_file($path);
			
			/*time*/
			$datestring = "%Y%m%d_%h%i";
			$time = time();
			
			$time_n = mdate($datestring, $time);
			
			$this->zip->download('kmcs_'.$time_n.'.zip'); 
		}
}